package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.bean.PersonImpression;
import com.lishe.lishe.lishe.module.excitation.presenter.MinePresenter;
import com.lishe.lishe.lishe.widget.CircleImageView;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DELL on 2017/10/27.
 * 个人印象
 */

public class PersonImpressionActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity, String userId, String name,String imgUrl) {
        Intent intent = new Intent(activity, PersonImpressionActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra("name", name);
        intent.putExtra("imgUrl", imgUrl);
        activity.startActivity(intent);
    }

    private static final String TAG = PersonImpressionActivity.class.getSimpleName();
    private final int[] ID_IMPRESSON = new int[]{R.id.impression1, R.id.impression2, R.id.impression3, R.id.impression4, R.id.impression5};
    private final int[] ID_NUM = new int[]{R.id.num1, R.id.num2, R.id.num3, R.id.num4, R.id.num5};

    private Button mBack;
    private TextView mTitle;
    private CircleImageView mHead;
    private TextView[] mImpression = new TextView[5];
    private TextView[] mNum = new TextView[5];

    private String mUserId, mName,mImgUrl;
    private List<PersonImpression> personImpressions=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_impression);
        mUserId = getIntent().getStringExtra("userId");
        mName = getIntent().getStringExtra("name");
        mImgUrl = getIntent().getStringExtra("imgUrl");
        initView();
        initListener();
        getPersonImpression();
    }


    private void initView() {
        mBack = findViewById(R.id.title_left_button);
        mTitle = findViewById(R.id.title_text_title);
        mHead = findViewById(R.id.head);

        for (int i = 0; i < ID_IMPRESSON.length; i++) {
            mImpression[i] = findViewById(ID_IMPRESSON[i]);
            mNum[i] = findViewById(ID_NUM[i]);
        }


        mTitle.setText(mName + getString(R.string.impression));
        if(!TextUtils.isEmpty(mImgUrl)) {
            ImageLoaderUtils.displayImage(this,mImgUrl, mHead,R.mipmap.head);
        }
    }

    private void initListener() {
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_button:
                finish();
                break;
        }
    }

    /**
     * 获取个人印象
     */
    private void getPersonImpression() {
        try {
            MinePresenter.getInstance().getUserActionList(Globals.getToken(), mUserId, new DataResponseListener<List<PersonImpression>>() {
                @Override
                public void onSuccess(List<PersonImpression> dataResponse) {
                    personImpressions.addAll(dataResponse);
                    updateImpresson();
                }

                @Override
                public void onFail(String failMessage) {
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            toast(getString(R.string.data_parse_fail));
        }

    }

    private void updateImpresson() {
        for (int i = 0; i < personImpressions.size(); i++) {
            if(i<5) {
                mImpression[i].setVisibility(View.VISIBLE);
                mImpression[i].setText(personImpressions.get(i).getActionName());
                mNum[i].setVisibility(View.VISIBLE);
                mNum[i].setText("+" + String.valueOf(personImpressions.get(i).getNum()));
            }
        }
    }

}
